package com.cgbl.service.ui

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.ProjectListAdapter
import com.cgbl.service.databinding.AppActivityProjctListBinding
import com.cgbl.service.vm.ProjectListViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.ProjectBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

@Route(path = AppConstants.Router.Project.A_PROJECT_LIST)
class ProjectListActivity : GLBaseActivity<AppActivityProjctListBinding, ProjectListViewModel>() {

    lateinit var projectListAdapter: ProjectListAdapter

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_projct_list

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        projectListAdapter = ProjectListAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = projectListAdapter
        }
        projectListAdapter.setOnItemClickListener { adapter, view, position ->
            val id = projectListAdapter.data[position].id
            ARouterUtil.goToActivityWithBundle(
                AppConstants.Router.Project.A_MY_PROJECT_DETAIL,
                Bundle().apply {
                    putString(AppConstants.BundleKey.ID, id)
                })
        }
        binding.etSearch.addTextChangedListener(afterTextChanged = {
            viewModel.refresh()
        })
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
        viewModel.refresh()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "项目列表"
        backView?.setOnClickListener { finish() }
    }

    override fun initViewObservable() {
        viewModel.uc.refreshProjectDataEvent.observe(this) {
            projectListAdapter.setNewInstance(it as MutableList<ProjectBean>?)
            finishLoad()
        }
        viewModel.uc.loadMoreProjectDataEvent.observe(this) {
            projectListAdapter.addData(it)
            finishLoad()
        }
        LiveDataEventManager.observerRefreshProjectListEvent(this) {
            viewModel.refresh()
        }
    }

    private fun finishLoad() {
        binding.refreshLayout.finishLoadMore()
        binding.refreshLayout.finishRefresh()
    }
}